package com.pagepub.core.tag.validator;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.pagepub.core.utils.ExtendString;

public class ValidateNomalKeywordMoreFun extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1629199036951454925L;

	private String callfuntionanames;

	private String functionname;

	private String rightimage;

	private String submitvalidate;

	private String rightwordcolor;

	private String functions;

	public String getFunctions() {
		return functions;
	}

	public void setFunctions(String functions) {
		this.functions = functions;
	}

	public String getFunctionname() {
		return functionname;
	}

	public void setFunctionname(String functionname) {
		this.functionname = functionname;
	}

	public String getRightimage() {
		return rightimage;
	}

	public void setRightimage(String rightimage) {
		this.rightimage = rightimage;
	}

	public String getRightwordcolor() {
		return rightwordcolor;
	}

	public void setRightwordcolor(String rightwordcolor) {
		this.rightwordcolor = rightwordcolor;
	}

	public String getSubmitvalidate() {
		return submitvalidate;
	}

	public void setSubmitvalidate(String submitvalidate) {
		this.submitvalidate = submitvalidate;
	}

	public String getCallfuntionanames() {
		return callfuntionanames;
	}

	public void setCallfuntionanames(String callfuntionanames) {
		this.callfuntionanames = callfuntionanames;
	}

	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		StringBuffer str = new StringBuffer();
		ValidateEmpty validateempty = new ValidateEmpty();
		validateempty.setWordcolor(wordcolor);
		validateempty.setWrongimage(wrongimage);

		ValidateLength validateLength = new ValidateLength();
		validateLength.setWordcolor(wordcolor);
		validateLength.setWrongimage(wrongimage);

		// 将多函数以，分割
		if (this.functions != null && !"".equalsIgnoreCase(this.functions)) {
			// System.out.println(this.functions);
			if (this.functions.indexOf(",") > -1) {
				String[] temparray = this.functions.split(",");
				String[] temparray0 = this.callfuntionanames.split(",");
				for (int i = 0; i < temparray.length; i++) {

					if (temparray[i] != null
							&& !"".equalsIgnoreCase(temparray[i])) {
						IsExit isexit = new IsExit();
						if (temparray0[i] != null
								&& !"".equalsIgnoreCase(temparray0[i]))
							;
						isexit.setCallfuntionaname(ExtendString
								.nullToSpaceNoTrim(temparray0[i]));
						this.setFunctionname("validatenormalkeywordexit"
								+ temparray[i]);
						isexit.setFunctionname(functionname);
						isexit.setSubmitvalidate(submitvalidate);
						isexit.setRightimage(rightimage);
						isexit.setWordcolor(wordcolor);
						isexit.setWrongimage(wrongimage);
						isexit.setRightwordcolor(rightwordcolor);
						str.append(isexit.isexit());
						str.append(validatenomalkeyword(ExtendString
								.nullToSpaceNoTrim(temparray[i]),
								this.functionname));
					}
				}
			} else {
				IsExit isexit = new IsExit();
				isexit.setCallfuntionaname(callfuntionanames);
				this.setFunctionname("validatenormalkeywordexit"
						+ this.functions);
				isexit.setFunctionname(functionname);
				isexit.setSubmitvalidate(submitvalidate);
				isexit.setRightimage(rightimage);
				isexit.setWordcolor(wordcolor);
				isexit.setWrongimage(wrongimage);
				isexit.setRightwordcolor(rightwordcolor);
				str.append(isexit.isexit());
				str.append(validatenomalkeyword(ExtendString
						.nullToSpaceNoTrim(this.functions), this.functionname));
			}

		}

		// TODO 自动生成方法存根
		str.append(validateempty.validateempty());
		str.append(validateLength.validatelength());

		try {

			out.print(str);
			// System.out.println(str);
		} catch (IOException e) {
			// TODO 自动生成 catch 块

			e.printStackTrace();
		}
		return 1;
	}

	public StringBuffer validatenomalkeyword(String function,
			String functionname) {
		StringBuffer str = new StringBuffer();
		// String emptyelement="";
		String pluselement = "";
		if (this.submitvalidate.equalsIgnoreCase("true")) {
			// emptyelement="emptyelement(realname);";
			pluselement = "else pluselement(realname);";
		}
		str.append("<script language=\"javascript\">");
		// realvalue当前验证值 spanname 显示span 的id realname显示的名称 nextelmentname
		// 下一个要验证的元素
		str
				.append("function "
						+ function
						+ "(realvalue,minlenth,maxlenth,spanname,realname,callfunvalue) {");
		// str.append("alert(callfunvalue);");
		str.append("if(Validateempty(realvalue,realname,spanname))");
		str
				.append(" if(ValidateLength(realvalue,minlenth,maxlenth,realname,spanname))");
		str
				.append(functionname
						+ "(realvalue,spanname,realname,callfunvalue);");
		str.append(pluselement);
		str.append(pluselement);
		str.append("}");
		str.append("</script>");
		return str;
	}
}
